package com.glorysoft.tonglingxjpda.ui.function.view

import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseActivity
import com.glorysoft.tonglingxjpda.databinding.ActivityTrackBinding
import com.glorysoft.tonglingxjpda.entity.BatchListEntity
import com.glorysoft.tonglingxjpda.entity.BatchListEntity.BatchInfoEntity
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.ui.function.viewmodel.TrackViewModel
import com.pda.platform.ui.ui_pdaplatform.activity_public.FreeUI_QRCodeActivity
import kotlinx.android.synthetic.main.activity_title.*
import kotlinx.android.synthetic.main.activity_track.*

/**
 * FileName: TrackInOrOutActivity
 * Author: XJH
 * Date: 2021/9/15 10:20
 * Description:
 * History:
 */
class TrackInOrOutActivity : BaseActivity<TrackViewModel, ActivityTrackBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_track
    }

    override fun initVariableId(): Int {
        return BR.trackViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        val showPlugin = intent.getStringExtra("showPlugin")
        when (showPlugin) {
            "trackIn" -> tvTitle.text = "进站"
            "trackOut" -> tvTitle.text = "出站"
        }
        setRightListener("提交") {
            if (etBatchID.text.toString().isEmpty()) {
                viewModel.defUI.toastEvent.value = ToastMessageBean("请输入或扫描BatchID", false)
                return@setRightListener
            }

            viewModel.submit(etBatchID.text.toString(), tvTitle.text.toString())
        }

        viewModel.defUI.startActivity.observe(this) { finish() }
    }

    override fun initData() {

        tvScan.setOnClickListener {
            val intent = Intent(this, FreeUI_QRCodeActivity::class.java)
            //设置横屏
            intent.putExtra("screenOrientation", 1)
            startActivityForResult(intent, 101)
        }

        initEdit(etBatchID, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.getInfo(s)
            }

        })

        viewModel.defUI.callObserve.observe(this) {
            val batchInfoEntity = it.item as BatchInfoEntity
            val intent = Intent(this, DataCollectionActivity::class.java)
            val bundle = Bundle()
            bundle.putSerializable("DATA", batchInfoEntity)
            when (it.type) {
                0 -> {
                    intent.putExtras(bundle)
                    startActivityForResult(intent, 102)
                }
                1 -> {
                    startActivity(FinCheckActivity::class.java,bundle)
                }
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            if (requestCode == 101) {
                val barCode = data!!.getStringExtra("barCode")
                etBatchID.setText(barCode)
            }
            viewModel.getInfo(etBatchID.text.toString())
        }
    }
}